package com.zmn.plat.dubbo.impl.online.service.group;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.online.service.group.OnlineServiceGroupDIO;
import com.zmn.plat.dubbo.interfaces.online.service.group.OnlineServiceGroupListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroup;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroupQuery;
import com.zmn.plat.services.interfaces.online.service.group.OnlineServiceGroupService;
import com.zmn.plat.utils.bean.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author linfeng
 * @description
 * @modifier
 * @since 2021/4/7 17:03
 **/
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 5000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OnlineServiceGroupListRemoteServiceImpl implements OnlineServiceGroupListRemoteService {
    @Resource
    private OnlineServiceGroupService onlineServiceGroupService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<String>> listByOnlineServiceGroupDIO(OnlineServiceGroupDIO onlineServiceGroupDIO) {
        if (Objects.isNull(onlineServiceGroupDIO) || Objects.isNull(onlineServiceGroupDIO.getSysId()) || Objects.isNull(onlineServiceGroupDIO.getPlat())
        || Objects.isNull(onlineServiceGroupDIO.getSubCompanyId())) {
            log.error("listByOnlineServiceGroupDIO参数不能为空：onlineServiceGroupDIO：{}", onlineServiceGroupDIO);
            return ResponseDTO.fail("参数不能为空");
        }

        List<String> result = new ArrayList<>();

        OnlineServiceGroupQuery query = new OnlineServiceGroupQuery();
        BeanCopyUtils.copyProperties(onlineServiceGroupDIO, query);
        query.setStatus(GlobalConsts.YES);
        List<String> serviceGroupIdList = onlineServiceGroupService.listServiceGroupIdByQuery(query);
        if (CollectionUtil.isNullOrEmpty(serviceGroupIdList)) {
            OnlineServiceGroupQuery serviceGroupQuery = new OnlineServiceGroupQuery();
            serviceGroupQuery.setSysId(onlineServiceGroupDIO.getSysId());
            serviceGroupQuery.setSubCompanyId(onlineServiceGroupDIO.getSubCompanyId());
            serviceGroupQuery.setDefaultType(GlobalConsts.YES);
            serviceGroupQuery.setStatus(GlobalConsts.YES);
            List<OnlineServiceGroup> list = onlineServiceGroupService.listByQuery(serviceGroupQuery);
            if (CollectionUtil.isNotNullOrEmpty(list)) {
                result = list.stream().map(e -> e.getServiceGroupId()).collect(Collectors.toList());
            }
        } else {
            result = serviceGroupIdList;
        }

        return ResponseDTO.success(result);
    }
}